package com.chenjiaru.thread.lock;

import lombok.extern.slf4j.Slf4j;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @description: Condition实现的生产者消费者案例（生产衣服）
 * @author: chenjiaru
 * @create: 2022-06-28
 */
@Slf4j
public class ConditionDemo {
    /**锁*/
    public static final Lock lock = new ReentrantLock();
    /**生产者的Condition*/
    private static final Condition producerCondition = lock.newCondition();
    /**消防者的Condition*/
    private static final Condition consumerCondition = lock.newCondition();
    /**衣服库存*/
    public static final Queue<Clothes> queue = new LinkedList<>();
    /**衣服库存容量*/
    public static int capacity = 100;

    public static void main(String[] args) {
        for (int i = 1; i <= 3; i++) {
            new Thread(new Producer(queue, producerCondition, consumerCondition, lock), i + "号生产者").start();
            new Thread(new Consumer(queue, producerCondition, consumerCondition, lock), i + "号【消费者】").start();
        }
    }

}

/**
 * 衣服
 */
class Clothes {}

/**
 * 生产者线程
 */
@Slf4j
class Producer implements Runnable{
    /**衣服库存*/
    private Queue<Clothes> queue;
    /**生产者的Condition*/
    private Condition producerCondition;
    /**消防者的Condition*/
    private Condition consumerCondition;
    /**锁*/
    private Lock lock;
    public Producer(Queue<Clothes> queue, Condition producerCondition, Condition consumerCondition, Lock lock) {
        this.queue = queue;
        this.producerCondition = producerCondition;
        this.consumerCondition = consumerCondition;
        this.lock = lock;
    }
    @Override
    public void run() {
        while (true) {
            produce();
        }
    }
    private void produce() {
        try{
            lock.lock();
            while(queue.size() == ConditionDemo.capacity) {
                log.info("衣服库存已满停止生产，等待消费");
                producerCondition.await();
            }
            queue.offer(new Clothes());
            log.info("生产了一件衣服，衣服库存={}，唤醒一个消费者线程", queue.size());
            consumerCondition.signal();
            Thread.sleep(100);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

/**
 * 消费者线程
 */
@Slf4j
class Consumer implements Runnable{
    /**衣服库存*/
    private Queue<Clothes> queue;
    /**生产者的Condition*/
    private Condition producerCondition;
    /**消防者的Condition*/
    private Condition consumerCondition;
    /**锁*/
    private Lock lock;
    public Consumer(Queue<Clothes> queue, Condition producerCondition, Condition consumerCondition, Lock lock) {
        this.queue = queue;
        this.producerCondition = producerCondition;
        this.consumerCondition = consumerCondition ;
        this.lock = lock;
    }

    @Override
    public void run() {
        while(true) {
            consume();
        }
    }

    private void consume() {
        try{
            lock.lock();
            while(queue.size() == 0) {
                log.info("衣服库存为零，等待生产");
                consumerCondition.await();
            }
            queue.poll();
            log.info("消费了一件衣服，衣服库存={}，唤醒一个生产者者线程", queue.size());
            producerCondition.signal();
            Thread.sleep(100);
        }catch(Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
